<?php if (!defined('BASEPATH'))
    exit ('No direct script access allowed');
/**
 *    专辑相关
 * @author xiahuiqin
 *
 */
class Cloth extends Admin_Controller
{
    public function  __construct()
    {
        parent::__construct();

        $this->load->model("cloth_mdl", 'cloth');
        $this->load->model("common_mdl", 'common');
        $this->load->model("baby_type_mdl", 'baby_type');
        $this->load->model("column_mdl", 'column');
    }

    /**
     * 专辑列表
     */
    function cloth_list()
    {
        $data['result'] = $this->cloth->get_list();
        $this->layout->view('cloth/cloth_listV', $data);
    }

    /**
     * 宝贝属性
     */
    function baby_type_list(){

        $data['result'] = $this->baby_type->get_list();

        $this->layout->view('admin/admin/baby_type_listV', $data);
    }

    /**
     * 新增宝贝属性
     */
    function baby_type_add()
    {
        $datas          = array();
        $datas['error'] = '';

        if ($_POST) {
            $data['name'] = $this->input->post('name');
            $data['cloth_class_id'] = $this->input->post('cloth_class_id');
            $data['addtime'] = date("Y-m-d H:i:s");

            if ($this->baby_type->insert($data)) {
                $datas['error'] = "新增成功！";
            } else {
                $datas['error'] = '新增失败！';
            }
            $this->layout->view('admin/admin/baby_type_addV', $datas);

        } else {
            $this->layout->view('admin/admin/baby_type_addV', $datas);
        }
    }

    /**
     * 编辑宝贝属性
     */
    function baby_type_edit()
    {
        $id            = $this->uri->segment('4');
        $data['error'] = '';

        if ($_POST) {
            $datas['name'] = $this->input->post('name');
            $datas['cloth_class_id'] = $this->input->post('cloth_class_id');

            if ($this->baby_type->update_byfield('id', $id, $datas)) {
                $data['error'] = "信息编辑成功！";
            } else {
                $data['error'] = '数据库繁忙，请稍后重试。';
            }
        }
        $data['one']  = $this->baby_type->get_one_list($id);

        $this->layout->view('admin/admin/baby_type_editV', $data);
    }

    /**
     * 删除宝贝属性
     */
    function baby_type_delete()
    {
        $id  = $this->uri->segment(4);
        $con = array('id' => $id);

        $this->baby_type->del($con);

        $this->baby_type_list();
    }



    /**
     * 共用模板列表
     */
    function common_list($sign = '')
    {
        $where = array('sign' => $sign);

        $data['result'] = $this->common->get_list($where);
        $data['sign']   = $sign;

        $this->layout->view('admin/admin/common_listV', $data);
    }

    /**
     * 新增服装
     */
    function cloth_add()
    {
        $datas          = array();
        $datas['error'] = '';

        if ($_POST) {
            $this->form_validation->set_rules('name', '用户名', 'trim|required|max_length[15]');
            $this->form_validation->set_rules('password', '密码', 'trim|required');
            $this->form_validation->set_rules('cpassword', '确认密码', 'trim|required|matches[password]');
            $this->form_validation->set_rules('email', '邮箱', 'required');

            if ($this->form_validation->run() == FALSE) {

                $this->layout->view('cloth/cloth_addV', $datas);
            } else {
                $datas             = $this->_post_data();
                $datas['reg_time'] = date('Y-m-d H:i:s');
                $datas['img']      = $this->_upload_img('img', 'local_first_add');

                if ($this->cloth->insert($datas)) {
                    $datas['error'] = "成功成为会员！";
                } else {
                    $datas['error'] = '新增失败！';
                }
                $this->layout->view('cloth/cloth_addV', $datas);
            }
        } else {
            $this->layout->view('cloth/cloth_addV', $datas);
        }
    }

    /**
     * 共用信息(添加)
     */
    function common_add($sign = '')
    {
        $data          = array();
        $data['error'] = '';
        $data['sign']  = $sign;

        if ($_POST) {
            $datas['name'] = $this->input->post('name');
            $datas['sign'] = $this->input->post('sign');

            if ($this->common->insert($datas)) {
                $data['error'] = "信息录入数据库成功。";
            } else {
                $data['error'] = "服务器繁忙，请稍后重试。";
            }
            $data['sign'] = $this->input->post('sign');
        }

        $this->layout->view('admin/admin/common_addV', $data);
    }

    /**
     * 服装编辑
     */
    function cloth_edit()
    {
        $id = $this->uri->segment('4');

        if ($_POST) {
            $datas             = $this->_post_data();
            $datas['reg_time'] = $this->input->post('reg_time');
            $datas['img']      = $this->_upload_img('img', '1');

            if ($this->cloth->update_byfield('id', $id, $datas)) {
                $data['error'] = "更新会员信息成功！";
            } else {
                $data['error'] = '更新失败！';
            }
        } else {
            $data['error'] = '';
        }
        $data['one'] = $this->cloth->get_one_list($id);

        $this->layout->view('cloth/cloth_editV', $data);
    }

    /**
     * 共用模板信息编辑
     */
    function common_edit()
    {
        $id            = $this->uri->segment('4');
        $sign          = $this->uri->segment('5');
        $data['error'] = '';

        if ($_POST) {
            $datas['name'] = $this->input->post('name');
            $datas['sign'] = $this->input->post('sign');

            if ($this->common->update_byfield('id', $id, $datas)) {
                $data['error'] = "信息编辑成功！";
            } else {
                $data['error'] = '数据库繁忙，请稍后重试。';
            }
        }
        $data['one']  = $this->common->get_one_list($id);
        $data['sign'] = $sign;
        $this->layout->view('admin/admin/common_editV', $data);
    }

    /**
     * 会员删除
     */
    function cloth_delete()
    {
        $id  = $this->uri->segment(4);
        $con = array('id' => $id);

        $this->cloth->del($con);

        $this->cloth_list();
    }

    /**
     * 共用模板信息删除
     */
    function common_delete()
    {
        $id   = $this->uri->segment(4);
        $sign = $this->uri->segment(5);
        $con  = array('id' => $id);

        $this->$sign->del($con);

        $this->common_list($sign);
    }

    function _post_data()
    {
        $datas['name']  = $this->input->post('name'); //用户名
        $datas['phone'] = $this->input->post('phone'); //联系电话
        $datas['email'] = $this->input->post('email'); //邮箱
        $datas['url']   = $this->input->post('url'); //个人链接
        $datas['title'] = $this->input->post('title'); //头衔

        $datas['gender']     = $this->input->post('gender'); //性别
        $datas['birth_date'] = $this->input->post('birth_date'); //出生日期
        $datas['address']    = $this->input->post('address'); //地址

        if (strlen($this->input->post('password')) == 32) {
            $datas['password'] = $this->input->post('password'); //密码
        } else {
            $datas['password'] = md5($this->input->post('password')); //密码
        }
        if ($this->input->post('authority')) {
            $datas['authority'] = $this->input->post('authority'); //权限
        } else {
            $datas['authority'] = 0;
        }
        if ($this->session->userdata('authority') != 1) {
            $datas['last_login_time'] = date('Y-m-d H:i:s'); //上次登入时间,即将这次的登入时间记作下次用的上次登入时间
            $datas['last_login_ip']   = $this->input->ip_address(); //上次登入ip,放在登录那一块儿做修改
        }

        return $datas;
    }

    /*
     *
    * 搜索信息
    *
    */
    function search()
    {
        if ($this->input->post('keywords') == '') {
            $sign   = '';
            $limit  = '';
            $offset = '';
            $order  = 'id ASC';

            $data['result'] = $this->cloth->get_list($sign, $limit, $offset, $order);
        } else {
            $keywords = trim($this->input->post('keywords'));
            $super    = '超级后台';
            $normal   = '普通前台';
            $stop     = '暂被罢职前后台';
            //0是前台管理员，普通管理员，1是后台管理员，是超级管理员，2是前后台管理员，是暂被罢职管理员
            if (strstr($super, $keywords)) {
                $keywords = 1;
            } else if (strstr($normal, $keywords)) {
                $keywords = 0;
            } else if (strstr($stop, $keywords)) {
                $keywords = 2;
            } else {
                $keywords = $this->input->post('keywords');
            }
            $like  = array($this->input->post('sign') => $keywords);
            $order = 'id ' . $this->input->post('orderby');

            $data['result'] = $this->cloth->get_list('', '', '', $order, $like);
        }
        $this->layout->view('cloth/cloth_listV', $data);
    }

}